package com.frozenc.controller;

import com.frozenc.domain.Category;
import com.frozenc.domain.News;
import com.frozenc.domain.User;
import com.frozenc.mapper.CategoryMapper;
import com.frozenc.mapper.NewsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by FrozenC on 2016/9/8.
 */
@Controller
@SessionAttributes("currUser")
@RequestMapping(value = {"/","","/index"})
public class Index {

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @RequestMapping("")
    public String index(Model model) {

        List<Category> categories = categoryMapper.selectAllCategories();

        model.addAttribute("categories",categories);

        model.addAttribute("category_id",0);

        List<News> newsList = newsMapper.selectLastNews(10);

        model.addAttribute("newsList",newsList);

        return "index";
    }

    @RequestMapping("/c/{id}")
    public String index(@PathVariable Integer id,Model model) {

        List<Category> categories = categoryMapper.selectAllCategories();

        model.addAttribute("categories",categories);

        model.addAttribute("category_id",id);

        List<News> newsList = newsMapper.selectLastNewsByCategoryId(10,id);

        model.addAttribute("newsList",newsList);

        return "index";
    }
}
